﻿using System.Collections.Generic;
using System.Windows.Forms;
using Fiddler;
using FiddlerFirePHPInspector.BusinessObjects.FirePHP;
using FiddlerFirePHPInspector.BusinessObjects.Wildfire;
using FiddlerFirePHPInspector.Processors;

namespace FiddlerFirePHPInspector.FiddlerExtension
{
    public class FirePHPInspector : Inspector2, IResponseInspector2
    {
        private FirePHPInspectorUserControl firePhpInspectorUserControl;

        public void Clear()
        {
        }

        public byte[] body
        {
            get { return null; }
            set
            {
                //Do nothing
            }
        }

        public bool bDirty { get; set; }
        public bool bReadOnly { get; set; }

        public HTTPResponseHeaders headers
        {
            get { return null; }
            set
            {
                var wildfireDatas = WildfireProcessor.ExtractWildfireData(value);

                WildfireData firePHPWildfireData = null;
                foreach (var wildfireData in wildfireDatas)
                {
                    if (FirePHPProcessor.IsWildfireDataFirePHP(wildfireData))
                    {
                        firePHPWildfireData = wildfireData;
                        break;
                    }
                }

                if (firePHPWildfireData != null)
                {
                    List<FirePHPLog> firePHPLogs = FirePHPProcessor.ProcessFirePHPWildfireData(firePHPWildfireData);
                    firePhpInspectorUserControl.FirePHPLogs = firePHPLogs;
                }
                else
                {
                    firePhpInspectorUserControl.FirePHPLogs = null;
                }
            }
        }

        public override void AddToTab(TabPage o)
        {
            firePhpInspectorUserControl = new FirePHPInspectorUserControl();
            o.Text = "FirePHP";
            o.Controls.Add(firePhpInspectorUserControl);
            o.Controls[0].Dock = DockStyle.Fill;
        }

        public override int GetOrder()
        {
            return 0x42;
        }
    }
}


